package com.fineaiops.gateway.service;

import com.fineaiops.gateway.util.ConstString;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;

@Service
public class ExpService {
    public RestHighLevelClient client;

    public ExpService(@NotNull EsClient client) {
        this.client = client.getClient();
    }

    public static void main(String[] args) {
        try {
            //System.out.println(Arrays.toString(getExceptionCategories(new String[]{"ZnJvbnRlbmQ=.1"}, new long[]{}, new long[]{})));
            System.out.println("ok!");
        } catch (Exception e) {
            System.out.println(e);
        }
    }
    public SearchHit[] getExceptionCategories (
            String[] serviceID,
            long[] levels,
            long[] status
    ) throws IOException {
        SearchRequest searchRequest = new SearchRequest(ConstString.exceptionIndex);
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (serviceID.length > 0) {
            boolQueryBuilder.must(QueryBuilders.termsQuery("service_id.keyword",serviceID));
        }
        if (status.length > 0) {
            boolQueryBuilder.must(QueryBuilders.termsQuery("status",status));
        }
        if (levels.length > 0) {
            boolQueryBuilder.must(QueryBuilders.termsQuery("level",levels));
        }
        sourceBuilder.query(boolQueryBuilder)
                //.sort()
        ;
        searchRequest.source(sourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(searchResponse.getHits().getTotalHits().value);
        return searchResponse.getHits().getHits();
    }

    public GetResponse getExceptionCateByID(String id) throws IOException {
        GetRequest getRequest = new GetRequest(ConstString.exceptionIndex, id);
        return client.get(getRequest, RequestOptions.DEFAULT);
    }

    public GetResponse getLogByID(String id) throws IOException {
        GetRequest getRequest = new GetRequest(ConstString.logIndex, id);
        return client.get(getRequest, RequestOptions.DEFAULT);
    }

    public SearchHit[] getRelativeExceptionByContent(String content, ArrayList<String> serviceID) throws IOException{
        SearchRequest request = new SearchRequest(ConstString.exceptionIndex)
                .source(new SearchSourceBuilder()
                        .query(QueryBuilders.boolQuery()
                                .must(QueryBuilders.termsQuery("service_id.keyword", serviceID))
                                .must(QueryBuilders.matchQuery("content", content))
        ));
        SearchResponse response= client.search(request,RequestOptions.DEFAULT);
        return response.getHits().getHits();
    }
}
